package com.blockmeta.bbs.overallserviceapplication.search.adapter;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.businesslibrary.util.DeepLinkUtils;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.pojo.SearchBannerPOJO;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;


/**
 * Created by author on 2018/11/2.
 */

public class SearchHotNewAdapter extends BaseMultiItemQuickAdapter<SearchBannerPOJO.BannerBean.ListBean, BaseViewHolder> {
    private Context mContext;

    public SearchHotNewAdapter(List<SearchBannerPOJO.BannerBean.ListBean> data, Context context) {
        super(data);
        addItemType(SearchBannerPOJO.BannerBean.ListBean.TypeTag1, R.layout.item_search_hot_tag1);
        addItemType(SearchBannerPOJO.BannerBean.ListBean.TypeTag2, R.layout.item_search_hot_tag2);
        mContext = context;
    }

    private void initLinerView(BaseViewHolder helper, final SearchBannerPOJO.BannerBean.ListBean item) {
        if (item.getTitle() != null) {
            helper.setText(R.id.item_tv_name, item.getTitle());
        }

        helper.getView(R.id.ll_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String regexUri = item.getDeeplink();
                if (TextUtils.isEmpty(regexUri)){return;}
                DeepLinkUtils.jumpToTarget(mContext, Uri.parse(regexUri));
            }
        });

    }

    //兼容币种和交易所
    private void initGridView(BaseViewHolder helper, final SearchBannerPOJO.BannerBean.ListBean item) {

        ImageView icon = helper.getView(R.id.item_image_icon);
        if (!item.getLogo().isEmpty()) {
            Glide.with(mContext).load(item.getLogo()).into(icon);
        } else {
            icon.setImageResource(R.drawable.default_header);
        }
        helper.setText(R.id.item_tv_name, item.getTitle()).addOnClickListener(R.id.ll_main);


        if (item.getType().equals("币种")) {
        } else if (item.getType().equals("交易所")) {
            helper.getView(R.id.ll_main).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DeepLinkUtils.jumpToTarget(mContext, Uri.parse(item.getDeeplink()));
                }
            });

        }
    }


    @Override
    protected void convert(BaseViewHolder helper, SearchBannerPOJO.BannerBean.ListBean item) {
        int rank = helper.getLayoutPosition() + 1;
        TextView tvRank = helper.getView(R.id.item_tv_rank);
        tvRank.setText(rank + "");
        if (rank == 1) {
            tvRank.setTextColor(Color.parseColor("#EE5653"));
        } else if (rank == 2) {
            tvRank.setTextColor(Color.parseColor("#FF9D41"));
        } else if (rank == 3) {
            tvRank.setTextColor(Color.parseColor("#FFCC3D"));
        } else {
            tvRank.setTextColor(Color.parseColor("#999999"));
        }
        switch (helper.getItemViewType()) {
            case SearchBannerPOJO.BannerBean.ListBean.TypeTag1:
                initGridView(helper, item);
                break;
            case SearchBannerPOJO.BannerBean.ListBean.TypeTag2:
                initLinerView(helper, item);
                break;
            default:
                break;
        }
    }


}
